package com.xxx.video.commen.views;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.xxx.video.commen.utils.ListUtils;

import java.util.List;

import butterknife.ButterKnife;

public abstract class TagAdapter<T> {
    /**
     * data
     */
    public List<T> tagList;
    /**
     * item click call back
     */
    public OnItemClickListener onItemClickListener;

    public TagAdapter(List<T> data) {
        this.tagList = data;
    }

    /**
     * get total item count
     *
     * @return total count
     */
    public int getItemCount() {
        if (ListUtils.isEmpty(tagList)) {
            return 0;
        }
        return tagList.size();
    }

    /**
     * get item data
     *
     * @param position position
     * @return data
     */
    public T getItemData(int position) {
        if (ListUtils.isEmpty(tagList)) {
            return null;
        }
        return tagList.get(position);
    }

    /**
     * get view
     *
     * @param position position
     * @param parent   parent
     * @return view
     */
    public View getView(int position, ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getItemLayout(), parent, false);
        ButterKnife.bind(this, view);
        initData2View(position, view);
        return view;
    }

    public interface OnItemClickListener<V> {
        /**
         * item click method
         *
         * @param v        item data
         * @param position position
         * @param type     type
         */
        void onItemClick(V v, int position, int type);
    }

    /**
     * set item click listener
     *
     * @param onItemClickListener call back
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * bind data to view
     *
     * @param position position
     * @param view     view
     */
    protected abstract void initData2View(int position, View view);

    /**
     * get item layout
     *
     * @return layout
     */
    protected abstract int getItemLayout();
}
